package com.lds.erp.gateway.runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.dph.common.utils.utils.AppUtils;
import com.dph.common.utils.utils.CommonUtils;
import com.dph.common.utils.utils.DESUtils;
import com.lds.erp.common.util.AccountCache;
import com.lds.erp.common.util.AccountUtils;
import com.lds.erp.configuration.ConfigProperty;

/**
 * @author hongsir
 * @apiNote
 * @date 2018/10/30 3:50 PM
 */
@Order(1)
@Component
public class InitRunner implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(InitRunner.class);
    @Autowired
    private ConfigProperty configProperty;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void run(String... strings) {
        // 设置开发账号
        setDevUser();
    }

    private void setDevUser() {
        // 非开发模式不初始化账号
        if (!configProperty.getDeveloperMode()) {
            return;
        }
        // 设置用户token,数据规则必须同User服务一致
        String mchId = "b6828e9c2ccc4db5ab46d92ca3dfa4b1";//AppUtils.newOrderNo();
        String token = DESUtils.encrypt(mchId + "_" + CommonUtils.getId());
        log.info("虚拟一个30分钟有效期的token:\n{}", token);

        AccountCache cache = new AccountCache();
        cache.setMchImage("defalutImg");
        cache.setMchName("LSC");
        cache.setMchId(mchId);
        cache.setMchToken(token);
        cache.setMchContactMobile("18523036721");

        AccountUtils.cacheAccount(cache);

    }

}
